// 导入createRouter方法和创建History路由模式的createWebHistory方法
import { createRouter, createWebHistory } from 'vue-router'
// 导入登录页
import Login from '@/views/login/index.vue'
// import Setting from '@/views/settings/index.vue'
import { useUsernameStore } from '@/store/usernameStore'
import { ElMessage } from 'element-plus'
 

// 具体各个路由项
const routes = [
    {
        path: '/',
        name: 'root',
        redirect: '/home' // 根路由重定向到主页
    },
    {
        path: '/login',
        name: 'login',
        component: Login
    },
    {
        path: '/home',
        name: 'home',
        component: () => import('@/views/home/index.vue')
    },
    {
        path: '/todo',
        name: 'todo',
        component: () => import('@/views/todo/index.vue')
    }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})
router.beforeEach((to, from) => {
    // 全局路由守卫，待补充
    const usernameStore = useUsernameStore()
    if (usernameStore.username == 'Anonymous' && to.path != '/login')
        {
            ElMessage.info('请先登入！')
            return '/login'
        }
        return true
})

export default router
